/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bookingtickets;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

/**
 *
 * @author alexxxey
 */

@Entity
@Table(name = "TRIPS")
@XmlRootElement
@NamedQueries(
{
    @NamedQuery(name = "Trip.findAll", query = "SELECT t FROM Trip t"),
    @NamedQuery(name = "Trip.findById", query = "SELECT t FROM Trip t WHERE t.id = :id"),
    @NamedQuery(name = "Trip.findByTripdate", query = "SELECT t FROM Trip t WHERE t.tripdate = :tripdate"),
    @NamedQuery(name = "Trip.findByTriptime", query = "SELECT t FROM Trip t WHERE t.triptime = :triptime"),
    @NamedQuery(name = "Trip.findByPlacesnum", query = "SELECT t FROM Trip t WHERE t.placesnum = :placesnum")    
})

public class Trip implements Serializable
{
    @Column(name = "TRIPTIME")
    @Temporal(TemporalType.TIME)
    private Date triptime;
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "ID")
    private Integer id;            
    @Basic(optional = false)        
    @NotNull    
    @Column(name = "TRIPDATE")    
    @Temporal(TemporalType.TIMESTAMP)    
    private Date tripdate;                                   
    @Basic(optional = false)
    @NotNull
    @Column(name = "PLACESNUM")
    private int placesnum;
    @OneToMany(mappedBy = "tripid", fetch = FetchType.EAGER)
    private List<Destination> destinationList;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "trip", fetch = FetchType.EAGER)
    private List<Ticket> ticketList;

    public Trip()
    {
    }

    public Trip(Integer id)
    {
        this.id = id;
    }

    public Trip(Integer id, Date tripdate, int placesnum)
    {
        this.id = id;
        this.tripdate = tripdate;
        this.placesnum = placesnum;
    }

    public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public Date getTripdate()
    {
        return tripdate;
    }

    public void setTripdate(Date tripdate)
    {
        this.tripdate = tripdate;
    }
              
    public int getPlacesnum()
    {
        return placesnum;
    }

    public void setPlacesnum(int placesnum)
    {
        this.placesnum = placesnum;
    }

    @XmlTransient
    public List<Destination> getDestinationList()
    {
        return destinationList;
    }

    public void setDestinationList(List<Destination> destinationList)
    {
        this.destinationList = destinationList;
    }

    @XmlTransient
    public List<Ticket> getTicketList()
    {
        return ticketList;
    }

    public void setTicketList(List<Ticket> ticketList)
    {
        this.ticketList = ticketList;
    }

    
    
    @Override
    public int hashCode()
    {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object)
    {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Trip))
        {
            return false;
        }
        Trip other = (Trip) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id)))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return "bookingtickets.Trip[ id=" + id + " ]";
    }
    
    /**
     * Получение числа свободных мест на маршруте
     * @return Текущее число свободных мест
     */
    public int getFreePlacesNum()
    {
        return placesnum - ticketList.size();
    }
    
    /**
     * Получение числа свободных мест на маршруте
     * @return Текущее число свободных мест
     */
    public List<Integer> getFreePlacesList()
    {
        ArrayList<Integer> places = new ArrayList<Integer>();
        
        for (int i=0; i<placesnum; ++i)
        {
            places.add(i+1);
        }
        
        for (Ticket t : ticketList)
        {
            places.remove(new Integer(t.getTicketPK().getPlace()));
        }
        
        return places;
    }

    public Date getTriptime()
    {
        return triptime;
    }

    public void setTriptime(Date triptime)
    {
        this.triptime = triptime;
    }
}
